package com.ua.secure.dictionary.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ua.common.model.Dictionary;
import com.ua.common.model.DictionaryItem;
import com.ua.common.model.PageResultBean;
import com.ua.open.common.service.CommonService;
import com.ua.secure.dictionary.service.DictionaryItemService;


/**
 * 字典条目控制器
 * @author zhaoruiqing
 * 2013-5-8
 */
@Controller
@RequestMapping("/secure/dictItem/*")
public class DictionaryItemController {
	@Autowired
	private DictionaryItemService dictItemService;
	
	private CommonService commonService;
	
	/**
	 * 显示数据字典页
	 * @return
	 */
	@RequestMapping("listDictItem.html")
	public ModelAndView listDictItem(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("dictItem", new DictionaryItem());
		mav.setViewName("secure/dictionary/listDictionaryItem");
		return mav;
	}
	
	@RequestMapping("qryPageDictItems.html")
	public @ResponseBody PageResultBean qryPageDictItems(Integer jtStartIndex,Integer jtPageSize,String dictId,String dictItemName,String dictItemCode){
		PageResultBean pr = new PageResultBean();
		try {
			DictionaryItem dictItem=new DictionaryItem();
			dictItem.setDictId(dictId);
			dictItem.setDictItemName(dictItemName);
			dictItem.setDictItemCode(dictItemCode);
			dictItem.setFromNumber(jtStartIndex);
			dictItem.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(dictItemService.selectPageDictItems(dictItem));
			pr.setTotalRecordCount(dictItemService.dictItemCount(dictItem));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		return pr;
	}
	
	/**
	 * 弹出新增对话框
	 * @return dictionaryItemForm.jsp
	 */
	@RequestMapping("initDictItem.html")
	public ModelAndView initDict(){
		ModelAndView mav = new ModelAndView();
		mav.addObject("dictItem",new Dictionary());
		mav.setViewName("secure/dictionary/dictItemForm");
		return mav;
	}
	
	@RequestMapping("saveDictItem.html")
	public @ResponseBody PageResultBean saveDictItem(DictionaryItem dictItem){
		PageResultBean pr=new PageResultBean();
		try {
			if(dictItem!=null){
				String dictId = dictItem.getDictItemId();
				if(dictId!=null && !"".equals(dictId)){
					dictItemService.updateDictItem(dictItem);
				}else{
					dictItem.setDictId(commonService.getCode());
					dictItemService.saveDictItem(dictItem);
				}
			}
			pr.setResult("OK");
			pr.setRecord(dictItem);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("保存失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="updateDictItem.html")
	public ModelAndView updateDictItem(String dictItemId){
		ModelAndView mav = new ModelAndView();
		mav.addObject("dictItem",dictItemService.selectDictItemById(dictItemId));
		mav.setViewName("secure/dictionary/dictionaryForm");
		return mav;
	}
	
	@RequestMapping("delDictItem.html")
	public @ResponseBody PageResultBean delDictItem(String dictItemId){
		PageResultBean pr = new PageResultBean();
		try {
			dictItemService.deleteDictItem(dictItemId);
			pr.setResult("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("删除失败!");
		}
		return pr;
	}
}
